package cn.itcast.cloud.storage;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import org.junit.Test;

import java.io.ByteArrayInputStream;

/**
 * 创建文件夹的用法，OSS文件夹的详细说明请参看文件夹
 */
public class CreateFolderSample {
    private static String endpoint = "http://oss-cn-zhangjiakou.aliyuncs.com";
    private static String accessKeyId = "LTAIXeZktAHXvAgc";
    private static String accessKeySecret = "iHpKZJ5KiB7imcohA7VgjDzLY2O87w";
    private static String bucketName = "jiaze";
    private static String key = "cloud";

    @Test
    public void testCreateFolder() throws Exception {
        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        final String keySuffixWithSlash = "MyObjectKey/";
        client.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
        System.out.println("Creating an empty folder " + keySuffixWithSlash + "\n");

        /*
             * Verify whether the size of the empty folder is zero
             */
        OSSObject object = client.getObject(bucketName, keySuffixWithSlash);
        System.out.println("Size of the empty folder '" + object.getKey() + "' is " +
                object.getObjectMetadata().getContentLength());
        object.getObjectContent().close();

        client.shutdown();
    }
}
